package pizza.admin.controller;

import java.io.IOException;
import java.util.Collection;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import pizza.admin.dao.PizzaDAO;
import pizza.admin.dao.PizzaDAO;
import pizza.admin.entity.Pizza;

/**
 * Servlet implementation class PizzaController
 */
@WebServlet("/pizza")
public class PizzaController extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public PizzaController() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		PizzaDAO pizzaDAO = new PizzaDAO();
		String action = request.getParameter("action");
		HttpSession session = request.getSession(true);
		Pizza pizza = new Pizza();
		Collection<Pizza> allPizzas = null;

		if (action != null) {
			if (session.getAttribute("user") == null) {
				request.getRequestDispatcher("/templates/index.jsp").forward(
						request, response);
			}
			if (action.equals("add")) {

				request.getRequestDispatcher("/templates/pizza/add.jsp")
						.forward(request, response);
			} else if (action.equals("edit")) {
				pizza = pizzaDAO.getById(Integer.parseInt(request
						.getParameter("id")));
				
				request.setAttribute("pizza", pizza);

				request.getRequestDispatcher("/templates/pizza/edit.jsp")
						.forward(request, response);
			} else if (action.equals("delete")) {

				pizza = pizzaDAO.getById(Integer.parseInt(request
						.getParameter("id")));
				pizzaDAO.delete(pizza);

//				pizzaDAO.initializeEntityManager();
				allPizzas = pizzaDAO.getAll();
				request.setAttribute("allPizzas", allPizzas);
				request.getRequestDispatcher("/templates/pizza/list.jsp")
						.forward(request, response);
			} else if (action.equals("list")||action.isEmpty()) {

				allPizzas = pizzaDAO.getAll();
				
				request.setAttribute("allPizzas", allPizzas);
				request.getRequestDispatcher("/templates/pizza/list.jsp")
						.forward(request, response);
			}
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		PizzaDAO pizzaDAO = new PizzaDAO();
		String action = request.getParameter("action");

		HttpSession session = request.getSession(true);
		Pizza pizza = new Pizza();

		if (action != null) {
			if (session.getAttribute("user") == null) {
				request.getRequestDispatcher("/templates/index.jsp").forward(
						request, response);
			}
			if (action.equals("add")) {

				pizza.setName(request.getParameter("name"));
				pizza.setDescription(request.getParameter("description"));
				pizza.setPrice(Double.parseDouble(request.getParameter("price")));
				

				if (!pizza.validateForDB()) {
					request.setAttribute("pizza", pizza);
					request.getRequestDispatcher("/templates/pizza/add.jsp")
							.forward(request, response);
				} else {
					pizzaDAO.add(pizza);
					Collection<Pizza> allPizzas = null;
//					pizzaDAO.initializeEntityManager();
					allPizzas = pizzaDAO.getAll();
					request.setAttribute("allPizzas", allPizzas);
					request.getRequestDispatcher("/templates/pizza/list.jsp")
							.forward(request, response);
				}
			} else if (action.equals("edit")) {
				pizza = pizzaDAO.getById(Integer.parseInt(request
						.getParameter("id")));

//				pizzaDAO.initializeEntityManager();
				
				pizza.setName(request.getParameter("name"));
				pizza.setDescription(request.getParameter("description"));
				pizza.setPrice(Double.parseDouble(request.getParameter("price")));

				if (!pizza.validateForDB()) {
					request.setAttribute("pizza", pizza);
					request.getRequestDispatcher("/templates/pizza/add.jsp")
							.forward(request, response);
				} else {
										
					pizzaDAO.edit(pizza);
					Collection<Pizza> allPizzas = null;
//					pizzaDAO.initializeEntityManager();
					allPizzas = pizzaDAO.getAll();
					request.setAttribute("allPizzas", allPizzas);
					request.getRequestDispatcher("/templates/pizza/list.jsp")
							.forward(request, response);
				}
			}
		}
	}

}
